import {
  upload_file,
  publish_food,
  serverUrl
} from '../../network/interface'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    images: [],
    name: '',
    score: "",
    description: ""
  },

  // onShow: function () {
  //   this.emptyData()
  // },

  emptyData(){
    this.setData({
      images:[],
      name: '',
      score: "",
      description: ""
    })
  },



  chooseimage: function () {
    wx.chooseImage({
      count: 6 - this.data.images.length,
      success: res => {
        this.setData({
          images: this.data.images.concat(res.tempFiles)
        })
      }
    })
  },


  // 批量上传图片
  batchUpload() {
    let urls = [];
    let arr = this.data.images;
    console.log("arr:"+arr[0].path)
    wx.uploadFile({
      filePath: arr[0].path,
      name: 'file',
      url: 'https://apibyte.wkhelpme.com/mars/upload/v1/img',
      success(res){
        console.log(JSON.parse(res.data).data)
        
      }
    })
    return urls
  },

  // 发布
  publish() {
    wx.showLoading({
      title: '发布中'
    })
    var that =this;
    let arr = this.data.images;
    console.log("arr:"+arr[0].path)
    wx.uploadFile({
      filePath: arr[0].path,
      name: 'file',
      url: 'https://apibyte.wkhelpme.com/mars/upload/v1/img',
      success(res){
        console.log(JSON.parse(res.data).data)
        let params = {
          name: that.data.name,
          description: that.data.description,
          score: that.data.score,
          imgUrl: JSON.parse(res.data).data
        };
        publish_food(params, res => {
          wx.hideLoading({
            success: (res) => {},
          })
          wx.navigateTo({
            url: '../cook/cook',
          })
          wx.showToast({
            title: '发布成功',
            icon:'none'
          })
        })
      }
    })
    

  }





})